﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;

namespace 图书管理系统
{
   public class booksDao
    {
        
       
        public int getkejiesum(String BookName)
        {
            int num = 0;
            SqlConnection conn = new SqlConnection();
            String sql = "select count(*) from Binfo_Table where BookName='" + BookName + "' and BookStatus='可借'";
            SqlCommand cmd = Dao.SqlConnect(sql, conn);
            try
            {
                   
                conn.Open();
                SqlDataReader sqlDataReader=cmd.ExecuteReader();
                while(sqlDataReader.Read())
                {
                    num = (int)sqlDataReader[0];
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message + "3");
            }
            finally
            {
                conn.Close();
            }
            return num;
        }
        public List<Book> GetBooks(String ISBN)
        {
            SqlConnection conn = new SqlConnection();
            String sql = "select * from Binfo_Table where ISBN='" + ISBN + "'";
            SqlCommand cmd = Dao.SqlConnect(sql, conn);
            List<Book> books = new List<Book>();
            try
            {
                conn.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                while(reader.Read())
                {
                    Book book = new Book();
                    book.ISBN1 = reader[0].ToString();
                    book.BookNum1 = reader[1].ToString();
                    book.BookName1 = reader[2].ToString();
                    book.Author1 = reader[3].ToString();
                    book.Press1 = reader[4].ToString();
                    book.Publication_time1 = (DateTime)reader[5];
                    book.BookIntro1 = reader[6].ToString();
                    book.BookLocation1 = reader[7].ToString();
                    book.BookStatus1 = reader[8].ToString();
                    if(reader[9].ToString()=="0")
                    {
                        book.Shelf1 =0;
                    }
                    else
                    {
                        book.Shelf1 = 1;
                    }
                    books.Add(book);
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message + "4");
            }
            finally
            {
                conn.Close();
            }
            return books;
        }
    }
}
